package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.OrderItemMapper;
import com.hxgz.steel.dao.OrderMapper;
import com.hxgz.steel.dao.QuotationMapper;
import com.hxgz.steel.domain.Order;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.Quotation;
import com.hxgz.steel.dto.OrderDetailDto;
import com.hxgz.steel.dto.OrderItemTimeDto;
import com.hxgz.steel.dto.OrderTimeDto;
import com.hxgz.steel.service.BackendOrderService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.hxgz.steel.service.NewOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;

@Service
public class BackendOrderServiceImp implements BackendOrderService {

  @Autowired
  private OrderMapper orderMapper;

  @Autowired
  private OrderItemMapper orderItemMapper;
  @Autowired
  private QuotationMapper quotationMapper;
@Autowired
private NewOrderService newOrderService;

  @Override
  public ServerResponse checkOrderList(HashMap<Object, Object> map, Page page) {
    PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
    ArrayList<OrderTimeDto> orderTimeDtos = orderMapper.checkOrderListByMySelfOrThird(map);
    PageInfo pageInfo = new PageInfo(orderTimeDtos);
    return ServerResponse.createBySuccess(pageInfo);
  }

  @Override
  public ServerResponse<OrderDetailDto> checkOrderListById(String id) {
    OrderDetailDto orderDetailDto = orderMapper.checkOrderList(id);
    Order order = orderMapper.selectByPrimaryKey(Long.valueOf(id));
    List<OrderItemTimeDto> orderItemTimeDtos =newOrderService.newGetOrderItemByOrderId(order.getId(),order.getQuotationId());
    BigDecimal bigDecimal = new BigDecimal(0);
    for (OrderItemTimeDto orderItemTimeDto : orderItemTimeDtos) {
      bigDecimal = bigDecimal.add(orderItemTimeDto.getPrice()==null?new BigDecimal("0"):orderItemTimeDto.getPrice());
    }
    if(order.getQuotationId()!=null){
        Quotation quotation = quotationMapper.selectByPrimaryKey(order.getQuotationId());
        bigDecimal = quotation.getTotalPrice();
        orderDetailDto.setAllPrice(bigDecimal);
    }
    orderDetailDto.setOrderItemList(orderItemTimeDtos);
    return ServerResponse.createBySuccess(orderDetailDto);
  }
}

